<?php

namespace app\admin\controller;

use app\BaseController;
use think\facade\Db;
use think\facade\Request;
use think\facade\Session;

class Menu extends BaseController
{
    /**
     * 菜单列表
     * @return string|\think\response\Redirect
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function index()
    {
        $login = Session::get('account');
        if(!Session::has('account')){
            return redirect('/admin/login');  //没有登录就跳转到登录页面
        }
        $left = Db::table('shop_menu')->where('fid',0)->select()->toArray();
        foreach($left as &$left_v){
            $left_v['lists'] = Db::table('shop_menu')->where('fid',$left_v['id'])->select();
        }
        $menu = Db::table("shop_menu")->select();
        MyViewAssign([
            'login' => $login,
            'left' => $left,
            'menu' => $menu,
        ]);
        return MyView();
    }

    /**
     * 菜单添加
     * @return string|void
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function add()
    {
        if(Request::method() == 'POST'){    //点击保存
            $all = Request::param();
            $insert = Db::table('shop_menu')->insert($all);
            if($insert){    //成功
                echo json_encode(['code'=>0,'msg'=>'添加成功']);
                exit;
            }else{  //失败
                echo json_encode(['code'=>1,'msg'=>'添加失败']);
                exit;
            }
        }else{  //第一次点击添加时
            $menu = Db::table('shop_menu')->where('fid',0)->select();
            MyViewAssign("menu",$menu);
            return MyView();
        }
    }

    /**
     * 菜单删除
     * @throws \think\db\exception\DbException
     */
    public function del()
    {
        $id = Request::param('id');
        $delete = Db::table('shop_menu')->where('id',$id)->delete();
        if($delete){    //成功
            echo json_encode(['code'=>0,'msg'=>'删除成功']);
            exit;
        }else{  //失败
            echo json_encode(['code'=>1,'msg'=>'删除失败']);
            exit;
        }
    }

    /**
     * 菜单编辑
     * @return string|void
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function edit()
    {
        if(Request::method() == 'POST'){   //保存执行修改操作
            $all = Request::param();
            $update = Db::table('shop_menu')->where('id',$all['id'])->update($all);
            if($update){    //成功
                echo json_encode(['code'=>0,'msg'=>'修改成功']);
                exit;
            }else{  //失败
                echo json_encode(['code'=>1,'msg'=>'修改失败']);
                exit;
            }
        }else{  //查询需要修改记录
            $id = Request::param('id');
            $menu = Db::table('shop_menu')->where('id',$id)->find();
            $cat = Db::table('shop_menu')->where('fid',0)->select();
            MyViewAssign([
                "menu" => $menu,
                "cat" => $cat,
            ]);
            return MyView();
        }
    }
}